home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-13 | 6.4 KB | 313 lines | [TEXT/ttxt] |
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- expanded class C_ARRAY[E]
- --
- -- Eiffel access to array of langage C.
- --
- -- Warning : using this class makes your Eiffel code non
- -- portable on others Eiffel systems.
- --
-
- feature -- Basic features :
-
- calloc(capacity: INTEGER) is
- -- Like the one of C ANSI using sizeof actual `E'.
- -- Values are set to the default Eiffel value.
- external "CSE"
- end;
-
- item(index: INTEGER): E is
- -- Assume that `malloc' is done and that `index' is the
- -- range [0 .. capacity-1].
- external "CSE"
- end;
-
- put(element: E; index: INTEGER) is
- -- Assume that `malloc' is done and that `index' is the
- -- range [0 .. capacity-1].
- external "CSE"
- end;
-
- feature
-
- malloc(capacity: INTEGER) is
- -- Like the one of C ANSI using sizeof actual `E'.
- -- As in C, values are not initialised.
- external "CSE"
- end;
-
- realloc(old_one: like Current; capacity: INTEGER) is
- -- Like the one of C ANSI using sizeof actual `E'.
- -- As in C, values are not initialised.
- external "CSE"
- end;
-
- feature -- Comparison :
-
- memcmp(other: like Current; capacity: INTEGER): BOOLEAN is
- -- True if all elements in range [0..capacity-1] are
- -- identical using `equal'. Assume Current and `other'
- -- are big enougth.
- -- See also `fast_memcmp'.
- require
- other.to_pointer.is_not_void;
- capacity > 0
- local
- i: INTEGER;
- do
- from
- i := capacity - 1;
- Result := equal_like(item(0),other.item(0));
- until
- i = 0 or else not Result
- loop
- Result := equal_like(item(i),other.item(i));
- i := i - 1;
- end;
- end;
-
- fast_memcmp(other: like Current; capacity: INTEGER): BOOLEAN is
- -- Same jobs as `memcmp' but uses infix "=" instead `equal'.
- require
- other.to_pointer.is_not_void;
- capacity > 0
- local
- i: INTEGER;
- do
- from
- Result := item(0) = other.item(0);
- i := capacity - 1;
- until
- i = 0 or else not Result
- loop
- Result := item(i) = other.item(i);
- i := i - 1;
- end;
- end;
-
- feature -- Searching :
-
- index_of(element: like item; upper: INTEGER): INTEGER is
- -- Give the index of the first occurrence of `element' using
- -- `is_equal' for comparison.
- -- Answer `upper + 1' when `element' is not inside.
- require
- upper >= -1
- do
- from
- until
- Result > upper or else equal_like(element,item(Result))
- loop
- Result := Result + 1;
- end;
- end;
-
- fast_index_of(element: like item; upper: INTEGER): INTEGER is
- -- Same as `index_of' but use `=' for comparison.
- require
- upper >= -1
- do
- from
- until
- Result > upper or else element = item(Result)
- loop
- Result := Result + 1;
- end;
- end;
-
- feature -- Removing :
-
- remove_first(upper: INTEGER) is
- -- Assume `upper' is a valid index.
- -- Move range [1 .. `upper'] by 1 position left.
- require
- upper >= 0
- local
- i: INTEGER;
- do
- from
- until
- i = upper
- loop
- put(item(i + 1),i);
- i := i + 1;
- end;
- end;
-
- remove(index, upper: INTEGER) is
- -- Assume `upper' is a valid index.
- -- Move range [`index' + 1 .. `upper'] by 1 position left.
- require
- index >= 0;
- index <= upper
- local
- i: INTEGER;
- do
- from
- i := index;
- until
- i = upper
- loop
- put(item(i + 1),i);
- i := i + 1;
- end;
- end;
-
- feature -- Other :
-
- set_all_with(v: like item; upper: INTEGER) is
- -- Set all elements in range [0 .. upper] with
- -- value `v'.
- local
- i: INTEGER;
- do
- from
- i := upper;
- until
- i < 0
- loop
- put(v,i);
- i := i - 1;
- end;
- end;
-
- copy_from(other: like Current; upper: INTEGER) is
- -- Assume `upper' is a valid index both in Current
- -- and `other'.
- local
- i: INTEGER;
- do
- from
- i := upper;
- until
- i < 0
- loop
- put(other.item(i),i);
- i := i - 1;
- end;
- end;
-
- nb_occurrences(element: like item; upper: INTEGER): INTEGER is
- -- Number of occurrences of `element' in range [0..upper]
- -- using `equal' for comparison.
- -- See also `fast_nb_occurrences' to chose the apropriate one.
- local
- i: INTEGER;
- do
- from
- i := upper;
- until
- i < 0
- loop
- if equal_like(element,item(i)) then
- Result := Result + 1;
- end;
- i := i - 1;
- end;
- end;
-
- fast_nb_occurrences(element: like item; upper: INTEGER): INTEGER is
- -- Number of occurrences of `element' in range [0..upper]
- -- using basic "=" for comparison.
- -- See also `fast_nb_occurrences' to chose the apropriate one.
- local
- i: INTEGER;
- do
- from
- i := upper;
- until
- i < 0
- loop
- if element = item(i) then
- Result := Result + 1;
- end;
- i := i - 1;
- end;
- end;
-
- hashcode(upper: INTEGER): INTEGER is
- -- Assume Current type is C_ARRAY[CHARACTER].
- local
- i: INTEGER;
- do
- from
- i := upper;
- if i > 15 then
- i := 15;
- end;
- until
- i < 0
- loop
- Result := Result + item(i).code;
- i := i - 1;
- end;
- end;
-
- all_cleared(upper: INTEGER): BOOLEAN is
- -- Are all items in range [0..upper] set to default
- -- values?
- require
- upper >= -1
- local
- i: INTEGER;
- model: like item;
- do
- from
- Result := true;
- i := upper;
- until
- i < 0 or else not Result
- loop
- Result := model = item(i)
- i := i - 1;
- end;
- end;
-
- feature -- The Guru section :
-
- free is
- -- Like the one of C ANSI.
- -- Assume Current is not already `free'.
- external "ICWC"
- end;
-
- feature -- Interfacing with C :
-
- to_external: POINTER is
- -- Gives access to the C pointer on the area of storage.
- do
- Result := to_pointer;
- end;
-
- from_pointer(c_array_pointer: POINTER) is
- -- Assign Current with `c_array_pointer'.
- external "CSE"
- end;
-
- is_not_void: BOOLEAN is
- do
- Result := to_pointer.is_not_void;
- end;
-
- feature {NONE}
-
- frozen equal_like(e1, e2: like item): BOOLEAN is
- -- Note: this feature is called to avoid calling `equal'
- -- on expanded types (no automatic conversion to
- -- corresponding reference type).
- do
- if e1.is_basic_expanded_type then
- Result := e1 = e2;
- elseif e1.is_expanded_type then
- Result := e1.is_equal(e2);
- elseif e1 = e2 then
- Result := true;
- elseif e1 = Void or else e2 = Void then
- else
- Result := e1.is_equal(e2);
- end;
- end;
-
- end
-